const Singleton = require("./Singleton");
const fs = require("fs");
const path = require("path");
const {getKeyID} = require("./Singleton");

const ImageTypesNames = {
    1: 'BMP',
    2: 'JPEG',
    3: 'GIF',
    4: 'PNG',
    5: 'TIFF',
    15: 'RAW'
}
class LocalList {
    constructor() {
        let files;
        this.local_list = [];
        files = fs.readdirSync(path.resolve(__dirname, './main'));
        for (const file of files) {
            this.local_list.push({
                keyID: Singleton.getKeyID(file.toLowerCase()),
                image_file_name: file
            })
        }
    }

    findImageFile(image_type, image_name) {
        let image_data = null;
        let keyID = getKeyID(`${image_name}.${ImageTypesNames[image_type]}`.toLowerCase());
        let file_item = this.local_list.find(item => item.keyID === keyID);

        if (file_item) {
            image_data = fs.readFileSync(path.resolve(__dirname, `./main/${file_item.image_file_name}`))
        }
        return image_data;

    }
}

module.exports = new LocalList();